<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\StatusOrder;
use App\Admin\Metrics\Examples\FormulaCard;
use App\Admin\Metrics\Examples\TotalAmountCard;
use App\Admin\Metrics\Examples\TotalCard;
use App\Admin\Metrics\Examples\TotalHandlingFeeCard;
use App\Admin\Metrics\Examples\TotalOrderCard;
use App\Admin\Metrics\Examples\TotalMissionDepositCard;
use App\Admin\Repositories\Order;
use App\Models\School;
use App\Models\UnitBlock;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\Model;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;

class OrderController extends AdminController
{

    protected $type = [1 => '跑腿', 2 => '其他任务'];

    protected $status = [2 => '待审核', 3 => '待接单', 4 => '审核失败', 5 => '已接单', 6 => '已完成', 7 => '已取消'];


    public function index(Content $content)
    {
        $page = request()->get('page', 1);
        $perPage = request()->get('per_page', 20);
        return $content
            ->header('任务')
            ->description('列表')
            ->body(function (Row $row) use ($perPage, $page) {
                $row->column(4, new TotalCard());
                $row->column(4, new TotalOrderCard($page, $perPage));
                $row->column(4, new TotalAmountCard($page, $perPage));
                $row->column(4, new TotalHandlingFeeCard($page, $perPage));
                $row->column(4, new TotalMissionDepositCard($page, $perPage));
                $row->column(4, new FormulaCard());
            })
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Order(['user', 'school', 'unitBlock', 'runErrand']), function (Grid $grid) {
            if (Admin::user()->school_id) {
                $grid->model()->where('school_id', Admin::user()->school_id);
            }
            $grid->model()->where('status', '!=', 1)->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('order_number');
            $grid->column('user.username');
            $grid->column('mobile');
            $grid->column('school.name');
            $grid->column('type')->using($this->type)->label([
                1 => Admin::color()->red(),
                2 => Admin::color()->success(),
            ]);
            $grid->column('amount');
            $grid->column('handling_fee');
            $grid->column('order_taking_fee');
            $grid->column('cut_off_at');
            $grid->column('pickup_code');
            $grid->column('write_off_code');
            $grid->column('write_off_code_url')->image('', 50, 50);
            $grid->column('images')->image('', 50, 50);
            $grid->column('status')->using($this->status)->label([
                1 => Admin::color()->gray(),
                2 => Admin::color()->gray(5),
                3 => Admin::color()->orange1(),
                4 => Admin::color()->red(),
                5 => Admin::color()->blue(),
                6 => Admin::color()->green(),
                7 => Admin::color()->dark90(),
            ]);
            $grid->column('runErrand.nick_name');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('order_number')->width(3);
                if (is_null(Admin::user()->school_id)) {
                    $filter->equal('school_id')->width(3)->select(School::all()->pluck('name', 'id'));
                }
                $filter->equal('type')->select($this->type)->width(3);
                $filter->equal('status')->select($this->status)->width(3);
            });
            $grid->showViewButton();
            $grid->disableEditButton();
            $grid->disableCreateButton();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if ($actions->row->status == 2) {
                    $actions->append(new StatusOrder());
                }
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Order(['user', 'school', 'unitBlock', 'runErrand']), function (Show $show) {
            $show->field('order_number');
            $show->field('type')->using($this->type);
            $show->field('user.username');
            $show->field('mobile');
            $show->field('school.name');
            $show->field('unitBlock.name');
            $show->field('room_number');
            $show->field('amount');
            $show->field('handling_fee');
            $show->field('order_taking_fee');
            $show->field('cut_off_at');
            $show->field('pickup_code');
            $show->field('write_off_code');
            $show->field('write_off_code_url')->image('', 50, 50);
            $show->field('description');
            $show->field('images')->image('', 50, 50);
            $show->field('status')->using($this->status);
            $show->field('runErrand.nick_name');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Order(), function (Form $form) {
            $form->display('id');
            $form->text('order_number');
            $form->text('user_id');
            $form->select('school_id')->options(School::all()->pluck('name', 'id'));
            $form->radio('type')->options($this->type);
            $form->text('unit_block_id')->options(UnitBlock::all()->pluck('name', 'id'));
            $form->text('room_number');
            $form->text('amount');
            $form->text('handling_fee');
            $form->text('order_taking_fee');
            $form->date('cut_off_at');
            $form->text('pickup_code');
            $form->textarea('description');
            $form->multipleImage('images')->move('images/order')->uniqueName()->autoUpload()->removable(false);
            $form->radio('status')->options($this->status);
        })->saving(function (Form $form) {
        });
    }
}
